import * as soundAlbumModel from '../../models/sound_album';
import * as soundVoiceModel from '../../models/sound_voice';
import * as soundCategoryModel from '../../models/sound_category';
import * as soundCategoryRelationModel from '../../models/sound_category_relation';
import * as userCollectModel from '../../models/user_collect';
import * as validHelp from '../../helps/valid';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* searchList({ search, page, number }) {
  if (!validHelp.Required(search)) throw ValidException('搜索词为空');
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['title', 'like', `${search}`]);
  let result = yield soundAlbumModel.search(where, page, number);
  return result;
}

export function* getCategoryList({ page, number }) {
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['status', '=', 1]);
  let result = yield soundCategoryModel.search(where, page, number, 'order');
  return result;
}

export function* getCategory({ category_id, page, number }) {
  if (!validHelp.Integer(category_id)) throw ValidException('分类id为空');
  let where = [];
  where.push(['category_id', '=', category_id]);
  let result = yield soundCategoryRelationModel.search(where, page, number);
  let ids = result.list.map(row => row.album_id);
  let album_list = yield soundAlbumModel.getIds(ids);
  return {
    count: result.count,
    list: album_list,
  };
}

export function* getAlbum({ album_id }) {
  if (!validHelp.Integer(album_id)) throw ValidException('参数为空');
  let album = yield soundAlbumModel.get(album_id);
  if (!album) throw EmptyException('绘本不存在');
  let list = yield soundVoiceModel.allByAlbum(album_id, 'order');
  album.list = list;
  return album;
}

export function* getCollectAlbumList({ robot_id, page, number }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器未绑定');
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['robot_id', '=', '']);
  where.push(['module', '=', 'sound_album']);
  let result = yield userCollectModel.search(where, page, number);
  return result;
}

export function* addCollectAlbum({ album_id, robot_id, user_id }) {
  if (!validHelp.Required(user_id)) throw ValidException('用户信息错误');
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(album_id)) throw ValidException('专辑为空');
  let album = yield soundAlbumModel.get(album_id);
  if (!album) throw EmptyException('专辑不存在');
  let relation = yield userCollectModel.getByRobotAndModel(robot_id, 'sound_album', album.id);
  relation = yield userCollectModel.add({ robot_id, user_id, module: 'sound_album', module_id: album.id });
  if (!relation) throw SystemException('添加收藏专辑失败');
  return {};
}

export function* deleteCollectAlbum({ album_id, robot_id }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(album_id)) throw ValidException('专辑信息错误');
  let relation = yield userCollectModel.getByRobotAndModel(robot_id, 'sound_album', album_id);
  if (!relation) throw EmptyException('未添加专辑收藏');
  yield userCollectModel.del(relation.id);
  return {};
}

export function* getCollectVoiceList({ robot_id, page, number }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器未绑定');
  if (!validHelp.Page(page, number)) throw ValidException('分页参数错误');
  let where = [];
  where.push(['robot_id', '=', '']);
  where.push(['module', '=', 'sound_voice']);
  let result = yield userCollectModel.search(where, page, number);
  return result;
}

export function* addCollectVoice({ user_id, robot_id, voice_id }) {
  if (!validHelp.Required(user_id)) throw ValidException('用户信息错误');
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(voice_id)) throw ValidException('音频为空');
  let voice = yield soundVoiceModel.get(voice_id);
  if (!voice) throw EmptyException('音频不存在');
  let relation = yield userCollectModel.getByRobotAndModel(robot_id, 'sound_voice', voice.id);
  relation = yield userCollectModel.add({ robot_id, user_id, module: 'sound_voice', module_id: voice.id });
  if (!relation) throw SystemException('添加收藏音频失败');
  return {};
}

export function* deleteCollectVoice({ robot_id, voice_id }) {
  if (!validHelp.Required(robot_id)) throw ValidException('机器人信息错误');
  if (!validHelp.Required(voice_id)) throw ValidException('音频信息错误');
  let relation = yield userCollectModel.getByRobotAndModel(robot_id, 'sound_voice', voice_id);
  if (!relation) throw EmptyException('未添加专辑收藏');
  yield userCollectModel.del(relation.id);
  return {};
}
